package com.hxzy.controller.system;


import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.XzxmUser;
import com.hxzy.common.enums.AckCode;
import com.hxzy.system.service.XzxmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Api(tags = "西藏用户管理")
@RestController
@RequestMapping(value = "/api/system/xzxmuser")
public class XzxmUserController {

    @Autowired
    private XzxmUserService xzxmUserService;

    @ApiOperation( value = "查询用户数据")
    @GetMapping(value = "/data")
    public R search(){
        List<XzxmUser> search = this.xzxmUserService.search();
        return R.okHasData(search);
    }

    @ApiOperation(value = "用户新增")
    @PostMapping(value = "/add")
    public R add(@RequestBody @Valid XzxmUser xzxmUser){
        Boolean add = this.xzxmUserService.add(xzxmUser);
        return add ? R.build(AckCode.SUCCESS) : R.build(AckCode.SYSTEM_PARAM_FAIL);
    }

    @ApiOperation(value = "根据Id查询用户数据")
    @ApiImplicitParam(name = "用户ID", value = "userId", example = "1")
    @GetMapping(value = "/{userId}")
    public R selectById(@PathVariable(value = "userId") Long userId){
        XzxmUser xzxmUser = this.xzxmUserService.selectById(userId);
        return R.okHasData(xzxmUser);
    }

    @ApiOperation(value = "用户修改信息")
    @PostMapping(value = "/update")
    public R update(@RequestBody @Valid XzxmUser xzxmUser){
        boolean b = this.xzxmUserService.updateById(xzxmUser);
        return b ? R.build(AckCode.SUCCESS) : R.build(AckCode.SYSTEM_PARAM_FAIL);
    }

    @ApiOperation(value = "根据id删除数据")
    @GetMapping(value = "/delete/{userId}")
    public R delete(@PathVariable("userId") Long userId){
        XzxmUser xzxmUser = this.xzxmUserService.selectById(userId);
        if (Objects.nonNull(xzxmUser)){
            xzxmUser.setUserStatus(1);
        }
        boolean b = this.xzxmUserService.updateById(xzxmUser);
        return b? R.ok(): R.build(AckCode.SYSTEM_PARAM_FAIL);
    }
}
